﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Common;
using Module;
using BLL;

namespace App
{
    public partial class UserControl_Top : System.Web.UI.UserControl
    {

        private long threadId = 0;
        private long paperId = 0;
        private long categoryId = 0;
        private int userType = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            logoUrl.HRef = BLL.Common.Contexts.AppPath + "/Index.aspx";
            if (!Page.IsPostBack)
            {
                System.Web.HttpContext.Current.Session["url"] = Contexts.ThisPageUrlWithParams;
                pageInit();
                SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapresolve);
            }
            
        }

        private void pageInit()
        {
            System.Web.HttpContext.Current.Session["url"] = Contexts.ThisPageUrlWithParams;
            if (AppAuthHelper.SessionCheck())
            {
                this.LoginUl.Visible = true;
                this.vistorUl.Visible = false;

                UserProfileInfo userProfile = BUserProfile.getInstance().GetUserProfileInfo(AppAuthHelper.CurrentUserPassportId);
                lbl_user.Text = string.IsNullOrEmpty(userProfile.NickName) ? AppAuthHelper.CurrentUserInfo.UserName : userProfile.NickName;
            }
            else
            {
                this.LoginUl.Visible = false;
                this.vistorUl.Visible = true;
            }
        }


        #region new
        private SiteMapNode SiteMap_SiteMapresolve(object sender, SiteMapResolveEventArgs e)
        {
            getUrlParams();
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            currentNode.ReadOnly = false;
            SiteMapNode tempNode = currentNode;

            return changeParentNode(tempNode);
            //return currentNode;
        }
        private SiteMapNode changeParentNode(SiteMapNode currentNode)
        {
            if (null != currentNode.ParentNode)
            {
                //if (!currentNode.Url.ToLower().Contains("sectionsublist") && currentNode.Url.ToLower().Contains("sectionlist"))
                //{
                //    if (BLL.Global.Contexts.GetDecrypt(sectionType).ToLower().Equals(Enum.RelationChildType.Topic.ToString().ToLower()))
                //    {
                //        currentNode.Title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SYISection", AppAuthHelper.CurrentLanguage);//"分享的章节";
                //    }
                //    else if (BLL.Global.Contexts.GetDecrypt(sectionType).ToLower().Equals(Enum.RelationChildType.HomeWork.ToString().ToLower()))
                //    {
                //        currentNode.Title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.BLSection", AppAuthHelper.CurrentLanguage);//"博客的章节";
                //    }
                //    else if (BLL.Global.Contexts.GetDecrypt(sectionType).ToLower().Equals(Enum.RelationChildType.Survey.ToString().ToLower()))
                //    {
                //        currentNode.Title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SurveySection", AppAuthHelper.CurrentLanguage);
                //    }
                //    else
                //    {
                //        currentNode.Title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Section", AppAuthHelper.CurrentLanguage); //"章节";
                //    }
                //}
                //else
                //{
                //    string title = changeTitle(currentNode.Url);
                //    if (!string.IsNullOrEmpty(title))
                //    {
                //        currentNode.Title = title;
                //    }
                //    //else//后续将Else删掉
                //    //{
                //    //    currentNode.Title = title;
                //    //}
                //}
                currentNode.Url = changeUrl(currentNode.Url);
                changeParentNode(currentNode.ParentNode);

                if (string.IsNullOrEmpty(currentNode.ParentNode.Url) && null != currentNode.ParentNode.ParentNode)
                {
                    currentNode.ParentNode = currentNode.ParentNode.ParentNode;
                }
            }
            else
            {
                currentNode.Url = changeUrl(currentNode.Url);
                //return currentNode;
                //string title = changeTitle(currentNode.Url);
                //if (!string.IsNullOrEmpty(title))
                //{
                //    currentNode.Title = title;
                //}
                //else//后续将Else删掉
                //{
                //    currentNode.Title = title;
                //}
            }
            
            return currentNode;
        }
        //private string changeTitle(string url)
        //{
        //    string title = string.Empty;
        //    if (url.ToLower().Contains("topiclist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.TopicList", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("topiccommentlist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.TopicDetail", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("topicinfoedit.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.TopicModify", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("topicinfoadd.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.TopicCreate", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("homeworklist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.HomeworkList", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("bloglist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.BlogList", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("blogdetail.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.BlogDetail", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("blogadd.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.BlogCreate", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("blogedit.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.BlogModify", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("surveylist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SurveyList", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("takeasurvey/index.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Survey", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("takeasurvey/end.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SurveyEnd", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("takeasurvey/start.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SurveyStart", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("mysetting"))
        //    {
        //        //title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.User", AppAuthHelper.CurrentLanguage);
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.MySetting", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("kudolist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Kudos", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("co-creationlist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Contest", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("co-creationdetail.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.ContestComment", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("threadlist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Forum", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("threaddetail.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Post", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("threaddetailadd.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.AddPost", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("threadadd.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.AddThread", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("threadpostedit.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Contest", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("onlinechat.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.LiveChat", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("sectionsublist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.SYISubSection", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("index.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Home", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("categorylist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Home", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("boardlist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Navigator.Board", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("giftlist.aspx"))
        //    {
        //        title = BLL.Global.Language.LanguageHelper.GetLabel("Gift.GiftList", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("myproject") || url.ToLower().Contains("giftshow") || url.ToLower().Contains("inexpenrecord") || url.ToLower().Contains("pointintroduction") || url.ToLower().Contains("takecash"))
        //    {
        //        title = LanguageHelper.GetLabel("NewGift.LeftMenu1", AppAuthHelper.CurrentLanguage);
        //    }
        //    else if (url.ToLower().Contains("comfigcommentlist"))
        //    {
        //        if (HttpContext.Current.Request.Url.Query.ToLower().Contains("conftype=1"))
        //        {
        //            title = LanguageHelper.GetLabel("Category.Announcement", AppAuthHelper.CurrentLanguage);
        //        }
        //        else if (HttpContext.Current.Request.Url.Query.ToLower().Contains("conftype=2"))
        //        {
        //            title = LanguageHelper.GetLabel("Category.Reward", AppAuthHelper.CurrentLanguage);
        //        }
        //        else if (HttpContext.Current.Request.Url.Query.ToLower().Contains("conftype=3"))
        //        {
        //            title = LanguageHelper.GetLabel("Category.Suggestion", AppAuthHelper.CurrentLanguage);
        //        }
        //    }
        //    else if (url.ToLower().Contains("messagecomment"))
        //    {
        //        title = LanguageHelper.GetLabel("NewUser.MsgComment", AppAuthHelper.CurrentLanguage);
        //    }
        //    return title;
        //}
        private string changeUrl(string url)
        {
            if (url.ToLower().Contains("threadinfo") )
            {
                url = url + "?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(threadId.ToString());
            }
            else if (url.ToLower().Contains("famousperson"))
            {
                url = url + "?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(paperId.ToString());
            }
            else if (url.ToLower().Contains("famousdynamicraperlist"))
            {
                url = url + "?" + Contexts.RegionCategoryId + "=" + Contexts.GetEncrypt(categoryId.ToString());
            }
            else if (url.ToLower().Contains("famousdynamicraper"))
            {
                url = url + "?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(paperId.ToString());
            }
            else if (url.ToLower().Contains("famousnarrativepaperlist"))
            {
                url = url + "?" + Contexts.RegionCategoryId + "=" + Contexts.GetEncrypt(categoryId.ToString());
            }
            else if (url.ToLower().Contains("famousnarrativepaper"))
            {
                url = url + "?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(paperId.ToString());
            }
            else if (url.ToLower().Contains("memberrelationpaper"))
            {
                url = url + "?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(paperId.ToString());
            }
            else if (url.ToLower().Contains("policypager.aspx"))
            {
                url = url + "?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(paperId.ToString());
            }
            else if (url.ToLower().Contains("userlist"))
            {
                url = url + "?" + Contexts.UserType + "=" + Contexts.GetEncrypt(userType.ToString());
            }
            //else if (url.ToLower().Contains("sectionsublist"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + BLL.Global.Contexts.SectionType + "=" + Contexts.GetValueFromRequest(Contexts.SectionType) + "&" + Contexts.SectionId + "=" + Contexts.GetEncrypt(sectionId.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());//
            //}
            //else if (url.ToLower().Contains("sectionlist"))
            //{
            //    if (sectionId.ToString().Length > 10)
            //    {
            //        url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + BLL.Global.Contexts.SectionType + "=" + Contexts.GetValueFromRequest(Contexts.SectionType) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());//
            //    }
            //    else
            //    {
            //        url = "";
            //    }
            //}
            //else if (url.ToLower().Contains("shareyourideas") || url.Contains("shareYourIdeas"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + BLL.Global.Contexts.SectionType + "=" + Contexts.GetValueFromRequest(Contexts.SectionType) + "&" + Contexts.SectionId + "=" + Contexts.GetEncrypt(sectionId.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());//
            //}
            //else if (url.ToLower().Contains("livechat") || url.Contains("liveChat"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("cocreation") || url.Contains("coCreation"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("homeworklist"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.SectionId + "=" + Contexts.GetEncrypt(sectionId.ToString()) + "&" + BLL.Global.Contexts.SectionType + "=" + BLL.Global.Contexts.GetEncrypt(Enum.RelationChildType.HomeWork.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("blog") || url.Contains("blog"))
            //{
            //    url = url + "?" + Contexts.HwId + "=" + Contexts.GetEncrypt(homeWorkId.ToString()) + "&" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.SectionId + "=" + Contexts.GetEncrypt(sectionId.ToString()) + "&" + BLL.Global.Contexts.SectionType + "=" + BLL.Global.Contexts.GetEncrypt(Enum.RelationChildType.HomeWork.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("thread") || url.Contains("thread"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.BoardId + "=" + Contexts.GetEncrypt(Contexts.GetBoardId(false, string.Empty).ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("boardlist") || url.Contains("boardlist"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString()) + "&" + Contexts.CategoryId + "=" + Contexts.GetEncrypt(Contexts.GetAppCategoryId(false).ToString());
            //}
            //else if (url.ToLower().Contains("categorylist") || url.Contains("categorylist"))
            //{
            //    url = url + "?" + Contexts.PjId + "=" + Contexts.GetEncrypt(pjId.ToString());
            //}
            //else if (url.ToLower().Contains("user") || url.Contains("user"))
            //{

            //}
            //else if (url.ToLower().Contains("polls") || url.Contains("polls"))
            //{

            //}
            //else if (url.ToLower().Contains("kudo") || url.Contains("kudo"))
            //{

            //}
            //else if (url.ToLower().Contains("livechat"))
            //{

            //}
            //else if (url.ToLower().Contains("index") || url.Contains("index"))
            //{

            //}
            return url;
        }
        private void getUrlParams()
        {

            threadId = Contexts.GetAPPThreadId();
            paperId = Contexts.GetAPPPaperId();
            categoryId = Contexts.GetAPPRegionCategoryID();
            userType = Contexts.GetUserType();
        }

        #endregion
    }
}